package com.xca.is.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xca.is.R;
import com.xca.is.activity.SoftwareDetailsActivity;
import com.xca.is.entity.SoftwareEntity;
import com.xca.is.utils.NotificationUtils;

import java.util.List;

public class SoftwareAdapter extends RecyclerView.Adapter<SoftwareAdapter.ViewHolder>{
    private List<SoftwareEntity.DataDTO> list;
    private Context context ;

    public SoftwareAdapter(List<SoftwareEntity.DataDTO> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void updateData(List<SoftwareEntity.DataDTO> list){
        this.list = list ;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context , R.layout.software_item , null) ;
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SoftwareEntity.DataDTO data = list.get(position) ;
        Glide.with(context)
                .load(data.getPic())
                .into(holder.logo);
        holder.name.setText(data.getName());
        holder.version.setText(data.getVersion());
        holder.size.setText(data.getSize() + "Mb");
        holder.look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent  = new Intent(context , SoftwareDetailsActivity.class) ;
                Gson gson = new Gson() ;
                String software = gson.toJson(data) ;
                intent.putExtra("software", software);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return !list.isEmpty() ? list.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView logo ;
        TextView name , version  , size , look;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            logo = itemView.findViewById(R.id.software_item_img);
            name = itemView.findViewById(R.id.software_item_name);
            version = itemView.findViewById(R.id.software_item_version);
            size = itemView.findViewById(R.id.software_item_size);
            look = itemView.findViewById(R.id.software_item_look);
        }
    }
}
